package com.huike.game.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.R;
import com.huike.game.base.Bena22;

import java.util.ArrayList;

public class QiAdapter extends BaseAdapter {
    private static final String TAG = "log";
    RequestQueue queue=null;
    ArrayList<Bena22.DataBean> data=null;
    Context cc;
   public QiAdapter(ArrayList<Bena22.DataBean> a,Context context){
       data=a;

       cc=context;
       queue= Volley.newRequestQueue(cc);


   }


    /**
     * How many items are in the data set represented by this Adapter.
     *
     * @return Count of items.
     */
    @Override
    public int getCount() {
        return data.size();
    }

    /**
     * Get the data item associated with the specified position in the data set.
     *
     * @param position Position of the item whose data we want within the adapter's
     *                 data set.
     * @return The data at the specified position.
     */
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }


    @Override
    public long getItemId(int position) {
        return 0;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Bena22.DataBean dataBean=data.get(position);

        ViewCh viewCh=null;
        if(convertView==null){
            convertView = View.inflate(parent.getContext(), R.layout.gv_item, null);
            viewCh = new ViewCh(convertView);
            convertView.setTag(viewCh);
        }
        else {
          viewCh = (ViewCh) convertView.getTag();

        }
        Log.e(TAG, "getView: "+viewCh );


        Log.e(TAG, "getView: "+(viewCh.imageView==null) );
        viewCh.textView.setText(dataBean.getCarName());


        final ViewCh finalViewCh = viewCh;
        ImageRequest imageRequest = new ImageRequest(dataBean.getImage(), new Response.Listener<Bitmap>() {
            @Override
            public void onResponse( Bitmap bitmap) {
               finalViewCh.imageView.setImageBitmap(bitmap);

            }
        }, 200, 200,
                ImageView.ScaleType.CENTER,
                Bitmap.Config.ARGB_8888,
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {
                        Log.e(TAG, "onErrorResponse: "+volleyError.getMessage() );
                    }
                }
        );
        queue.add(imageRequest);
        Log.e(TAG, "getView:"+ dataBean.getCarName());


        return convertView;
    }
    static class ViewCh{
        ImageView imageView;
        TextView textView;
        ViewCh(View view){
            imageView = view.findViewById(R.id.iv_image);
            textView=  view.findViewById(R.id.gv_tv);
        }



    }

}
